package com.guiji.quartz.domain;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.json.JSONUtil;
import com.guiji.common.utils.spring.SpringUtils;
import com.guiji.quartz.formatter.DataFormatter;
import com.guiji.quartz.service.ISpiderNewsService;
import com.guiji.quartz.service.RedisService;
import lombok.Data;
import org.springframework.transaction.annotation.Transactional;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.model.AfterExtractor;
import us.codecraft.webmagic.model.annotation.ExtractBy;
import us.codecraft.webmagic.model.annotation.Formatter;
import us.codecraft.webmagic.model.annotation.HelpUrl;
import us.codecraft.webmagic.model.annotation.TargetUrl;

import java.util.Date;
import java.util.List;

/**
 * @program: cms-vue-plus
 * @description:
 * @author: gaoX
 * @create: 2021-08-26 10:43
 **/
@TargetUrl(value = "https://yule.360.com/detail/[0-9]*")
@HelpUrl({"https://yule.360.com/list?tag=%E7%94%B5%E5%BD%B1", "https://yule.360.com/list?tag=%E7%94%B5%E8%A7%86", "https://yule.360.com/list?tag=%E7%BB%BC%E8%89%BA",
	"https://yule.360.com/list?tag=%E6%98%8E%E6%98%9F"})
@Data
public class Zongyi360 implements AfterExtractor {
	@ExtractBy(value = "//h1/text()")
	private String title;

	@ExtractBy(value = "//div[@class='content']//p/text()")
	private List<String> content;

	@ExtractBy("//div[@class='content']//p//img/@src")
	private List<String> imgList;

	@Formatter(formatter = DataFormatter.class)
	@ExtractBy("//p[@class='source-time']/text()")
	private Date date;


	@Override
	public void afterProcess(Page page) {
		ISpiderNewsService service = SpringUtils.getBean(ISpiderNewsService.class);
		SpiderNews spider = new SpiderNews();
		spider.setNewsTitle(ChinaNews.filterSpecialCharacter(title));
		StringBuffer sb = new StringBuffer();
		content.forEach(s -> sb.append(s+"$$$"));
		String content = ChinaNews.filterSpecialCharacter(sb.toString());
		spider.setNewsContent(StrUtil.isEmpty(content) ? "" : content);
		spider.setContentSize(spider.getNewsContent().length());
		spider.setNewsPicJson(JSONUtil.toJsonStr(imgList));
		spider.setNewsUrl(page.getUrl().toString());
		spider.setNewsPublicTime(date);
		spider.setUpdateBy("admin");
		spider.setCreateTime(DateUtil.date());
		spider.setNewsSource("https://yule.360.com/");
		spider.setNewsCategory("360网综艺");
		System.out.println(title);
		System.out.println(content);
		System.out.println(date);

		if(title.contains("《")){
			return;
		}
		if (sb.length() < 5000 && sb.toString().replaceAll("\\$|[\\s\\p{Zs}]","").length()>20 && imgList.size() < 20) {
			RedisService redisService = SpringUtils.getBean(RedisService.class);
			Date ago = DateUtil.offsetDay(new Date(),-3);
			int i = date.compareTo(ago);
			//redis查询是否存在
			boolean ismember = redisService.isMember(SecureUtil.md5(title));
			if (!ismember&&i>0) {//如果不存在插入mysql和redis
				service.save(spider);
				redisService.addTitleSet(SecureUtil.md5(title), spider.getNewsCategory(), date,title);
			}
		}
	}
}
